package com.xl6667.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl6667.constants.SystemConstants;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.CommentDto;
import com.xl6667.domain.dto.PageDto;
import com.xl6667.domain.entity.Comment;
import com.xl6667.domain.entity.User;
import com.xl6667.enums.AppHttpCodeEnum;
import com.xl6667.exception.SystemException;
import com.xl6667.mapper.CommentMapper;
import com.xl6667.service.CommentService;
import com.xl6667.service.UserService;
import com.xl6667.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【sg_comment(评论表)】的数据库操作Service实现
* @createDate 2024-03-16 18:36:40
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
        @Autowired
        private UserService userService;

    @Override
    public ResponseResult commentList(String type,Integer articleId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //判断articleId是否为空
        queryWrapper.eq(type.equals(SystemConstants.ARTICLE_COMMENT),Comment::getArticleId,articleId);
        //查询评论类型
        queryWrapper.eq(Comment::getType,type);
        //先查根评论
        queryWrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOTID);
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Comment> records = page.getRecords();
        //转dto
        List<CommentDto> commentDtos = BeanCopyUtil.copyBeanList(records, CommentDto.class);
        //获取评论用户名称
        List<CommentDto> collect = getCommentDtosUserName(commentDtos);
        //查询子评论
        for (CommentDto dto : collect) {
            List<CommentDto> child  = getChild(dto.getId());
            dto.setChildren(child);
        }
        return ResponseResult.okResult(new PageDto(collect,page.getTotal()));

    }

    /**
     * 获取评论用户名称和头像
     * @return
     */
    private List<CommentDto> getCommentDtosUserName(List<CommentDto> commentDtos) {
        return commentDtos.stream().map(commentDto -> {
            //通过CreateBy获取用户昵称
            User user = userService.getById(commentDto.getCreateBy());
            commentDto.setUsername(user.getNickName());
            commentDto.setAvatar(user.getAvatar());
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if (commentDto.getToCommentUserId() != -1) {
                String userName = userService.getById(commentDto.getToCommentUserId()).getNickName();
                if (StringUtils.hasText(userName))
                    commentDto.setToCommentUserName(userName);
            }
            return commentDto;
        }).collect(Collectors.toList());
    }

    /**
     * 获取子评论
     * @param id
     * @return
     */
    private List<CommentDto> getChild(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        //没有子评论直接返回
        List<CommentDto> dtos = BeanCopyUtil.copyBeanList(list, CommentDto.class);
        //获取子评论用户名称
        List<CommentDto> childComments = getCommentDtosUserName(dtos);
        return childComments;

    }

    @Override
    public ResponseResult addComment(Comment comment) {
        String content = comment.getContent();
        if (!StringUtils.hasText(content)){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

}




